//
//  CPSHTackViewController.m
//  CardMoveGame
//
//  Created by peac on 2025/11/10.
//

#import "CPSHTackViewController.h"
#import "CustomTableViewCell.h"

// 定义数据模型
@interface CellData : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, strong) NSString *buttonTitle;
@end

@implementation CellData
@end

@interface CPSHTackViewController ()<CustomTableViewCellDelegate>

@end

@implementation CPSHTackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *drawbgIv=[[UIImageView alloc]initWithFrame:self.view.bounds];
    drawbgIv.contentMode = UIViewContentModeScaleAspectFill;
    drawbgIv.image=[UIImage imageNamed:@"bjtask"];
    [self.view addSubview:drawbgIv];
    
    [self setupData];
    [self setupTableView];
}

- (void)setupData {
    self.dataArray = @[
        [self createCellDataWithTitle:@"项目一" imageName:@"icon1" buttonTitle:@"详情"],
        [self createCellDataWithTitle:@"项目二" imageName:@"icon2" buttonTitle:@"编辑"],
        [self createCellDataWithTitle:@"项目三" imageName:@"icon3" buttonTitle:@"删除"],
        [self createCellDataWithTitle:@"项目四" imageName:@"icon4" buttonTitle:@"查看"]
    ];
}

- (CellData *)createCellDataWithTitle:(NSString *)title imageName:(NSString *)imageName buttonTitle:(NSString *)buttonTitle {
    CellData *data = [[CellData alloc] init];
    data.title = title;
    data.imageName = imageName;
    data.buttonTitle = buttonTitle;
    return data;
}

- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    // 注册自定义 Cell
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"CustomCell"];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
    
    CellData *data = self.dataArray[indexPath.row];
    
    // 配置单元格
    UIImage *image = [UIImage imageNamed:data.imageName];
    [cell configureWithImage:image title:data.title buttonTitle:data.buttonTitle];
    
    // 设置代理
    cell.delegate = self;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CellData *data = self.dataArray[indexPath.row];
    NSLog(@"选中了: %@", data.title);
}

#pragma mark - CustomTableViewCellDelegate

- (void)customCell:(CustomTableViewCell *)cell didTapButton:(UIButton *)button {
    // 获取点击的单元格索引
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    CellData *data = self.dataArray[indexPath.row];
    
    NSLog(@"点击了第 %ld 行的按钮: %@", (long)indexPath.row, data.buttonTitle);
    
    // 根据按钮标题执行不同的操作
    if ([data.buttonTitle isEqualToString:@"详情"]) {
        [self showDetailForItem:data.title];
    } else if ([data.buttonTitle isEqualToString:@"编辑"]) {
        [self editItem:data.title];
    } else if ([data.buttonTitle isEqualToString:@"删除"]) {
        [self deleteItemAtIndexPath:indexPath];
    } else if ([data.buttonTitle isEqualToString:@"查看"]) {
        [self viewItem:data.title];
    }
}

#pragma mark - 按钮操作处理方法

- (void)showDetailForItem:(NSString *)item {
    NSLog(@"显示详情: %@", item);
    // 实现详情显示逻辑
}

- (void)editItem:(NSString *)item {
    NSLog(@"编辑: %@", item);
    // 实现编辑逻辑
}

- (void)deleteItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"删除第 %ld 行", (long)indexPath.row);
    // 实现删除逻辑
}

- (void)viewItem:(NSString *)item {
    NSLog(@"查看: %@", item);
    // 实现查看逻辑
}


@end
